package com.chenshu.jdbc.dao;

import com.chenshu.jdbc.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-04-03
 * Time：10:55
 */
@Repository
public class StudentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void addStuInfo(Student student) {
        String sql = "insert into tb_student values(?,?,?,?)";
        jdbcTemplate.update(sql, new Object[] {student.getStuId(), student.getName(), student.getGender(), student.getAge()});
    }

    public List<Student> findAll() {
        String sql = "select * from tb_student";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
    }

    public Student findById(String stuId) {
        String sql = "select * from tb_student where stu_id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[] {stuId}, new BeanPropertyRowMapper<>(Student.class));
    }

}
